package tyk.Servlet;

import tyk.Service.OderItemService;
import tyk.Service.OrderService;
import tyk.pojo.AccountPage;
import tyk.pojo.Cart;
import tyk.pojo.Order;
import tyk.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/***
 *@title $NAME
 *@description <TODO description class purpose>
 *@author Tang'ya'kang
 *@VERSION 1.0.0
 *@CREATE 2023/5/18 17:39
 **/
public class OrderServlet extends HttpServlet {
    public void CreateOrder(HttpServletRequest request,HttpServletResponse response) throws SQLException, IOException {
        OrderService orderService=new OrderService();
        HttpSession session = request.getSession();
        User user= (User) session.getAttribute("User");
        Cart cart= (Cart) session.getAttribute("cart");
        String order_id=orderService.CreateOrder(cart,user);
        session.setAttribute("order_id",order_id);
        OderItemService oderItemService=new OderItemService();
        oderItemService.CreateOrderItems(order_id,user.getId(),cart);
        cart.Clear();
        response.sendRedirect("http://localhost:8080/JaveWeb/html/Order.jsp");
    }
    public void SelectMyOrderByUserid(HttpServletRequest request,HttpServletResponse response) throws SQLException, InstantiationException, IllegalAccessException, NoSuchFieldException, ServletException, IOException {
        Integer user_id= Integer.valueOf(request.getParameter("user_id"));
        List<Order> list=new ArrayList<>();
        list=new OrderService().SelectMyOrderByUserid(user_id);
        request.setAttribute("Orders",list);
        System.out.println(list);
        request.getRequestDispatcher("./html/MyOrder.jsp").forward(request,response);
    }
    public void SelectOrderRoot(HttpServletRequest request,HttpServletResponse response) throws SQLException, InstantiationException, IllegalAccessException, NoSuchFieldException, ServletException, IOException {
        Integer Page= Integer.valueOf(request.getParameter("Page"));
        Integer user_id= Integer.valueOf(request.getParameter("user_id"));
        OrderService orderService = new OrderService();
        List<Order>list=new ArrayList();
        AccountPage accountPage=new AccountPage();
        accountPage.setPage(Page);
        list=orderService.SelectOrderRoot(accountPage);
        request.setAttribute("Orders",list);
        request.setAttribute("AccountPage",accountPage);
        request.setAttribute("url","./orderServlet?action=SelectOrder&user_id="+user_id+"&user_name=root&Page=");
        request.getRequestDispatcher("./manage/RootOrders.jsp").forward(request,response);
    }

    public void SelectOrder(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

        String user_name=request.getParameter("user_name");
        Integer user_id= Integer.valueOf(request.getParameter("user_id"));
        Integer Page= Integer.valueOf(request.getParameter("Page"));
        if(user_name.equals("root")){
            request.getRequestDispatcher("./orderServlet?action=SelectOrderRoot&Page="+Page).forward(request,response);
            return;
        }else{
            request.getRequestDispatcher("./orderItemsServlet?action=SelectItemsByMerchan_id&Page="+Page+"&merchan_id="+user_id).forward(request,response);
            return;
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action=request.getParameter("action");
        try {
            Method method = this.getClass().getDeclaredMethod(action,HttpServletRequest.class,HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
